{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Домашнее задание к занятию \"Классификация: Логистическая регрессия и SVM\"\n",
    "\n",
    "Имеются данные adult.csv (см. в материалах к занятию).  \n",
    "Целевой переменной является уровень дохода income (крайний правый столбец).  \n",
    "Описание признаков можно найти по ссылке http://www.cs.toronto.edu/~delve/data/adult/adultDetail.html  \n",
    "Вам необходимо построить модели логистической регрессии и SVM, которые предсказывает уровень дохода человека.  \n",
    "Вывести качество полученных моделей на тестовой выборке, используя функцию score у модели.  \n",
    "Готовый ноутбук выложить на гитхаб и прислать ссылку."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>workclass</th>\n",
       "      <th>fnlwgt</th>\n",
       "      <th>education</th>\n",
       "      <th>educational-num</th>\n",
       "      <th>marital-status</th>\n",
       "      <th>occupation</th>\n",
       "      <th>relationship</th>\n",
       "      <th>race</th>\n",
       "      <th>gender</th>\n",
       "      <th>capital-gain</th>\n",
       "      <th>capital-loss</th>\n",
       "      <th>hours-per-week</th>\n",
       "      <th>native-country</th>\n",
       "      <th>income</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25</td>\n",
       "      <td>Private</td>\n",
       "      <td>226802</td>\n",
       "      <td>11th</td>\n",
       "      <td>7</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Machine-op-inspct</td>\n",
       "      <td>Own-child</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>38</td>\n",
       "      <td>Private</td>\n",
       "      <td>89814</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Farming-fishing</td>\n",
       "      <td>Husband</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>50</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>Local-gov</td>\n",
       "      <td>336951</td>\n",
       "      <td>Assoc-acdm</td>\n",
       "      <td>12</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Protective-serv</td>\n",
       "      <td>Husband</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>Private</td>\n",
       "      <td>160323</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Machine-op-inspct</td>\n",
       "      <td>Husband</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>7688</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103497</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Own-child</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age  workclass  fnlwgt     education  educational-num      marital-status  \\\n",
       "0   25    Private  226802          11th                7       Never-married   \n",
       "1   38    Private   89814       HS-grad                9  Married-civ-spouse   \n",
       "2   28  Local-gov  336951    Assoc-acdm               12  Married-civ-spouse   \n",
       "3   44    Private  160323  Some-college               10  Married-civ-spouse   \n",
       "4   18        NaN  103497  Some-college               10       Never-married   \n",
       "\n",
       "          occupation relationship   race  gender  capital-gain  capital-loss  \\\n",
       "0  Machine-op-inspct    Own-child  Black    Male             0             0   \n",
       "1    Farming-fishing      Husband  White    Male             0             0   \n",
       "2    Protective-serv      Husband  White    Male             0             0   \n",
       "3  Machine-op-inspct      Husband  Black    Male          7688             0   \n",
       "4                NaN    Own-child  White  Female             0             0   \n",
       "\n",
       "   hours-per-week native-country income  \n",
       "0              40  United-States  <=50K  \n",
       "1              50  United-States  <=50K  \n",
       "2              40  United-States   >50K  \n",
       "3              40  United-States   >50K  \n",
       "4              30  United-States  <=50K  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_adult = pd.read_csv('adult.csv', na_values=['?'])\n",
    "df_adult[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверим процент заполненности данных в каждом столбце"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Процент заполненности данных:\n",
      "age - 100.0%\n",
      "workclass - 99.94%\n",
      "fnlwgt - 100.0%\n",
      "education - 100.0%\n",
      "educational-num - 100.0%\n",
      "marital-status - 100.0%\n",
      "occupation - 99.94%\n",
      "relationship - 100.0%\n",
      "race - 100.0%\n",
      "gender - 100.0%\n",
      "capital-gain - 100.0%\n",
      "capital-loss - 100.0%\n",
      "hours-per-week - 100.0%\n",
      "native-country - 99.98%\n",
      "income - 100.0%\n"
     ]
    }
   ],
   "source": [
    "print('Процент заполненности данных:')\n",
    "for column_name in df_adult.columns:\n",
    "    percent = 100 - df_adult[column_name].isna().mean()\n",
    "    print(f'{column_name} - {round(percent,2)}%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Столбцы workclass, occupation и native-country имеют пропуски. Так как задача учебная и незаполненных данных достаточно мало, то заполним их медианой. \n",
    "\n",
    "Для этой операции требуется преобразовать тектовые категориальные данные в числовые. Преобразуем все текcтовые категориальные данные в нашем датафрейме."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LabelEncoder()"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "workclass_label_enc = LabelEncoder()\n",
    "workclass_label_enc.fit(df_adult.loc[~df_adult.workclass.isna(), 'workclass'])\n",
    "\n",
    "education_label_enc = LabelEncoder()\n",
    "education_label_enc.fit(df_adult.loc[~df_adult.education.isna(), 'education'])\n",
    "\n",
    "maritalstatus_label_enc = LabelEncoder()\n",
    "maritalstatus_label_enc.fit(df_adult.loc[~df_adult['marital-status'].isna(), 'marital-status'])\n",
    "\n",
    "occupation_label_enc = LabelEncoder()\n",
    "occupation_label_enc.fit(df_adult.loc[~df_adult.occupation.isna(), 'occupation'])\n",
    "\n",
    "relationship_label_enc = LabelEncoder()\n",
    "relationship_label_enc.fit(df_adult.loc[~df_adult.occupation.isna(), 'relationship'])\n",
    "\n",
    "race_label_enc = LabelEncoder()\n",
    "race_label_enc.fit(df_adult.loc[~df_adult.occupation.isna(), 'race'])\n",
    "\n",
    "gender_label_enc = LabelEncoder()\n",
    "gender_label_enc.fit(df_adult.loc[~df_adult.occupation.isna(), 'gender'])\n",
    "\n",
    "country_label_enc = LabelEncoder()\n",
    "country_label_enc.fit(df_adult.loc[~df_adult['native-country'].isna(), 'native-country'])\n",
    "\n",
    "income_label_enc = LabelEncoder()\n",
    "income_label_enc.fit(df_adult.loc[~df_adult.income.isna(), 'income'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_adult.loc[~df_adult.workclass.isna(), \n",
    "             'workclass'] = workclass_label_enc.transform(df_adult.loc[~df_adult.workclass.isna(), 'workclass'])\n",
    "df_adult.loc[~df_adult.education.isna(), \n",
    "             'education'] = education_label_enc.transform(df_adult.loc[~df_adult.education.isna(), 'education'])\n",
    "df_adult.loc[~df_adult['marital-status'].isna(), \n",
    "             'marital-status'] = maritalstatus_label_enc.transform(df_adult.loc[~df_adult['marital-status'].isna(), \n",
    "                                                                                'marital-status'])\n",
    "df_adult.loc[~df_adult.occupation.isna(), \n",
    "             'occupation'] = occupation_label_enc.transform(df_adult.loc[~df_adult.occupation.isna(), 'occupation'])\n",
    "df_adult.loc[~df_adult.relationship.isna(), \n",
    "             'relationship'] = relationship_label_enc.transform(df_adult.loc[~df_adult.relationship.isna(), 'relationship'])\n",
    "df_adult.loc[~df_adult.race.isna(), \n",
    "             'race'] = race_label_enc.transform(df_adult.loc[~df_adult.race.isna(), 'race'])\n",
    "df_adult.loc[~df_adult.gender.isna(), \n",
    "             'gender'] = gender_label_enc.transform(df_adult.loc[~df_adult.gender.isna(), 'gender'])\n",
    "\n",
    "df_adult.loc[~df_adult['native-country'].isna(), \n",
    "             'native-country'] = country_label_enc.transform(df_adult.loc[~df_adult['native-country'].isna(), \n",
    "                                                                          'native-country'])\n",
    "df_adult.loc[~df_adult.income.isna(), \n",
    "             'income'] = income_label_enc.transform(df_adult.loc[~df_adult.income.isna(), 'income'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Заменим отсутствующие данные в workclass, occupation и native-country медианой."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_adult.loc[df_adult.workclass.isna(), 'workclass'] = df_adult.workclass.median()\n",
    "df_adult.workclass = df_adult.workclass.astype(np.int32)\n",
    "df_adult.loc[df_adult.occupation.isna(), 'occupation'] = df_adult.occupation.median()\n",
    "df_adult.occupation = df_adult.occupation.astype(np.int32)\n",
    "df_adult.loc[df_adult['native-country'].isna(), 'native-country'] = df_adult['native-country'].median()\n",
    "df_adult['native-country'] = df_adult['native-country'].astype(np.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверим процент заполненности данных по всем столбцам."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Процент заполненности данных:\n",
      "age - 100.0%\n",
      "workclass - 100.0%\n",
      "fnlwgt - 100.0%\n",
      "education - 100.0%\n",
      "educational-num - 100.0%\n",
      "marital-status - 100.0%\n",
      "occupation - 100.0%\n",
      "relationship - 100.0%\n",
      "race - 100.0%\n",
      "gender - 100.0%\n",
      "capital-gain - 100.0%\n",
      "capital-loss - 100.0%\n",
      "hours-per-week - 100.0%\n",
      "native-country - 100.0%\n",
      "income - 100.0%\n"
     ]
    }
   ],
   "source": [
    "print('Процент заполненности данных:')\n",
    "for column_name in df_adult.columns:\n",
    "    percent = 100 - df_adult[column_name].isna().mean()\n",
    "    print(f'{column_name} - {round(percent,2)}%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Столбцы occupation и native-country являются номинальными категориями, поэтому их следует преобразовать в вектора с помощью One-Hot Encoding."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>workclass</th>\n",
       "      <th>fnlwgt</th>\n",
       "      <th>education</th>\n",
       "      <th>educational-num</th>\n",
       "      <th>marital-status</th>\n",
       "      <th>relationship</th>\n",
       "      <th>race</th>\n",
       "      <th>gender</th>\n",
       "      <th>capital-gain</th>\n",
       "      <th>...</th>\n",
       "      <th>occupation_Farming-fishing</th>\n",
       "      <th>occupation_Handlers-cleaners</th>\n",
       "      <th>occupation_Machine-op-inspct</th>\n",
       "      <th>occupation_Other-service</th>\n",
       "      <th>occupation_Priv-house-serv</th>\n",
       "      <th>occupation_Prof-specialty</th>\n",
       "      <th>occupation_Protective-serv</th>\n",
       "      <th>occupation_Sales</th>\n",
       "      <th>occupation_Tech-support</th>\n",
       "      <th>occupation_Transport-moving</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25</td>\n",
       "      <td>3</td>\n",
       "      <td>226802</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>38</td>\n",
       "      <td>3</td>\n",
       "      <td>89814</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>336951</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>160323</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7688</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "      <td>103497</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48837</th>\n",
       "      <td>27</td>\n",
       "      <td>3</td>\n",
       "      <td>257302</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48838</th>\n",
       "      <td>40</td>\n",
       "      <td>3</td>\n",
       "      <td>154374</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48839</th>\n",
       "      <td>58</td>\n",
       "      <td>3</td>\n",
       "      <td>151910</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48840</th>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>201490</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48841</th>\n",
       "      <td>52</td>\n",
       "      <td>4</td>\n",
       "      <td>287927</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>15024</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>48842 rows × 68 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       age  workclass  fnlwgt  education  educational-num  marital-status  \\\n",
       "0       25          3  226802          1                7               4   \n",
       "1       38          3   89814         11                9               2   \n",
       "2       28          1  336951          7               12               2   \n",
       "3       44          3  160323         15               10               2   \n",
       "4       18          3  103497         15               10               4   \n",
       "...    ...        ...     ...        ...              ...             ...   \n",
       "48837   27          3  257302          7               12               2   \n",
       "48838   40          3  154374         11                9               2   \n",
       "48839   58          3  151910         11                9               6   \n",
       "48840   22          3  201490         11                9               4   \n",
       "48841   52          4  287927         11                9               2   \n",
       "\n",
       "       relationship  race  gender  capital-gain  ...  \\\n",
       "0                 3     2       1             0  ...   \n",
       "1                 0     4       1             0  ...   \n",
       "2                 0     4       1             0  ...   \n",
       "3                 0     2       1          7688  ...   \n",
       "4                 3     4       0             0  ...   \n",
       "...             ...   ...     ...           ...  ...   \n",
       "48837             5     4       0             0  ...   \n",
       "48838             0     4       1             0  ...   \n",
       "48839             4     4       0             0  ...   \n",
       "48840             3     4       1             0  ...   \n",
       "48841             5     4       0         15024  ...   \n",
       "\n",
       "       occupation_Farming-fishing  occupation_Handlers-cleaners  \\\n",
       "0                               0                             0   \n",
       "1                               1                             0   \n",
       "2                               0                             0   \n",
       "3                               0                             0   \n",
       "4                               0                             0   \n",
       "...                           ...                           ...   \n",
       "48837                           0                             0   \n",
       "48838                           0                             0   \n",
       "48839                           0                             0   \n",
       "48840                           0                             0   \n",
       "48841                           0                             0   \n",
       "\n",
       "       occupation_Machine-op-inspct  occupation_Other-service  \\\n",
       "0                                 1                         0   \n",
       "1                                 0                         0   \n",
       "2                                 0                         0   \n",
       "3                                 1                         0   \n",
       "4                                 1                         0   \n",
       "...                             ...                       ...   \n",
       "48837                             0                         0   \n",
       "48838                             1                         0   \n",
       "48839                             0                         0   \n",
       "48840                             0                         0   \n",
       "48841                             0                         0   \n",
       "\n",
       "       occupation_Priv-house-serv  occupation_Prof-specialty  \\\n",
       "0                               0                          0   \n",
       "1                               0                          0   \n",
       "2                               0                          0   \n",
       "3                               0                          0   \n",
       "4                               0                          0   \n",
       "...                           ...                        ...   \n",
       "48837                           0                          0   \n",
       "48838                           0                          0   \n",
       "48839                           0                          0   \n",
       "48840                           0                          0   \n",
       "48841                           0                          0   \n",
       "\n",
       "       occupation_Protective-serv  occupation_Sales  occupation_Tech-support  \\\n",
       "0                               0                 0                        0   \n",
       "1                               0                 0                        0   \n",
       "2                               1                 0                        0   \n",
       "3                               0                 0                        0   \n",
       "4                               0                 0                        0   \n",
       "...                           ...               ...                      ...   \n",
       "48837                           0                 0                        1   \n",
       "48838                           0                 0                        0   \n",
       "48839                           0                 0                        0   \n",
       "48840                           0                 0                        0   \n",
       "48841                           0                 0                        0   \n",
       "\n",
       "       occupation_Transport-moving  \n",
       "0                                0  \n",
       "1                                0  \n",
       "2                                0  \n",
       "3                                0  \n",
       "4                                0  \n",
       "...                            ...  \n",
       "48837                            0  \n",
       "48838                            0  \n",
       "48839                            0  \n",
       "48840                            0  \n",
       "48841                            0  \n",
       "\n",
       "[48842 rows x 68 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_adult['native-country'] = country_label_enc.inverse_transform(df_adult['native-country'])\n",
    "df_adult.occupation = occupation_label_enc.inverse_transform(df_adult.occupation)\n",
    "df_adult = pd.get_dummies(df_adult, columns=['native-country', 'occupation'])\n",
    "df_adult"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Подготавливаем данные для использования логистической регрессии и SVM. Производим разделение на обучающую и тестовую выборки, выполняем стандартизацию всех данных."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df_adult.loc[:, df_adult.columns != 'income']\n",
    "y = df_adult['income']\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y)\n",
    "\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(X_train)\n",
    "\n",
    "X_train_std = scaler.transform(X_train)\n",
    "X_test_std = scaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Точность модели логистической регрессии: 0.83\n"
     ]
    }
   ],
   "source": [
    "lr = LogisticRegression(solver='lbfgs')\n",
    "lr.fit(X_train_std, y_train)\n",
    "y_predict = lr.predict(X_test_std)\n",
    "print(\"Точность модели логистической регрессии:\", round(accuracy_score(y_predict, y_test),2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Точность модели SVM: 0.84\n"
     ]
    }
   ],
   "source": [
    "svc = SVC()\n",
    "svc.fit(X_train_std, y_train)\n",
    "y_predict = svc.predict(X_test_std)\n",
    "print(\"Точность модели SVM:\", round(accuracy_score(y_predict, y_test),2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
